

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.net.InetSocketAddress;

public class CCannleDemo {
	public static void main(String[] args) throws IOException{
		//创建ssc对象
		ServerSocketChannel ssc = ServerSocketChannel.open();
		//设置通道为非阻塞模式
		ssc.configureBlocking(false);
		//绑定9999端口
		ssc.socket().bind(new InetSocketAddress(9999));
		//等待连接
		SocketChannel sc = null;
		while (sc == null){
			sc = ssc.accept();
		}
		//System.out.println("client is connected");
		//设置sc为非阻塞模式
		sc.configureBlocking(false);

		//读取头信息，获取体信息的长度
		String head = "";
		ByteBuffer temp = ByteBuffer.allocate(1);   //创建了几个缓冲对象，给1的长度，意思是一个字节一个字节的读
		while (!head.endsWith("\r\n")){
			//用temp读一个字节，把读到的字节+=head上，然后把temp清空
			sc.read(temp);
			head += new String(temp.array());
			temp.clear();
		}

		int len = Integer.parseInt(head.substring(0,head.length()-2));


		//开始创建指定长度的缓冲区来读取体的信息
		ByteBuffer buf = ByteBuffer.allocate(len);  //创建缓冲区对象为1kb
		while(buf.hasRemaining()){
			sc.read(buf);
		}

		//打印体的信息
		String str = new String(buf.array());
		System.out.println(str);

		while(true){}


	}


}